package com.jason.platform.supervision.log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName: LogFile
 * @Description: 日志文件管理
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-23 11:10:52
 */
public class LogFile {

    public LogFile() {
    }

    public static void writeLogAppend(String tag, String msg) {
        File file = new File(Log.FILE_PATH);
        if (!file.exists()) {
            String randomFile = Log.FILE_PATH.substring(0, Log.FILE_PATH.lastIndexOf("/"));
            File e = new File(randomFile);
            if (!e.exists()) {
                e.mkdirs();
            }

            try {
                file.createNewFile();
            } catch (IOException var8) {
                var8.printStackTrace();
            }
        }

        try {
            RandomAccessFile randomFile1 = new RandomAccessFile(Log.FILE_PATH, "rw");
            long e1 = randomFile1.length();
            if (e1 > (long) Log.FILE_MAX_LENGTH) {
                randomFile1.setLength(0L);
                e1 = 0L;
            }

            randomFile1.seek(e1);
            randomFile1.write(String.format("\n%s %s %s", new Object[]{getCurTime(), tag, msg}).getBytes("gb2312"));
            randomFile1.close();
        } catch (FileNotFoundException var6) {
            var6.printStackTrace();
        } catch (IOException var7) {
            var7.printStackTrace();
        }

    }

    private static String getCurTime() {
        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss.sss");
        Date date = new Date(System.currentTimeMillis());
        return format.format(date);
    }
}
